package com.bobi.tfca.ui.mainnavigation.user.password

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.constants.CodeCategoryEnum
import com.bobi.tfca.constants.ReplyConstant
import com.bobi.tfca.databinding.FragmentForgetPasswordBinding
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.MemberAPI
import com.bobi.tfca.util.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.lang.reflect.Member

class ForgetPasswordFragment : BaseFragment<FragmentForgetPasswordBinding, ViewModel>(
    FragmentForgetPasswordBinding::inflate,
    null,
    true
) {

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentForgetPasswordBinding,
        viewModel: ViewModel?,
        savedInstanceState: Bundle?
    ) {
        // 设置topPanel
        setTopPanelStyle(binding)
        // 设置下一步监听
        binding.btNextStep.setOnClickListener {
            val account = binding.etAccount.text.toString()
            val password = binding.etNewPassword.text.toString()
            if (account.isNullOrBlank() || password.isNullOrBlank()) {
                Toast.makeText(requireActivity(), "账号或密码不能为空", Toast.LENGTH_SHORT).show()
            } else if (password.length < 8) {
                Toast.makeText(requireActivity(), "密码长度必须大于8位", Toast.LENGTH_SHORT).show()
            } else {
                // 设置账号和密码
                publicViewModel!!.account = account
                publicViewModel!!.newPassword = password

                // 验证是否存在该账号
                checkAccountExist(account)
            }
        }
    }

    private fun checkAccountExist(account: String) {
        publicViewModel!!.apply {
            request(MemberAPI::class.java).getMemberByAccount(account).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }
                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val res = it.data
                                if (res?.code == 200) {
                                    // 跳转验证码
                                    publicViewModel!!.reply = ReplyConstant.RESET_PASSWORD
                                    publicViewModel!!.codeCategory = CodeCategoryEnum.RESET_PASSWORD_CODE
                                    publicViewModel!!.resetEmail = res.data ?: ""
                                    requireActivity().findNavController(R.id.app_nav)
                                        .navigate(R.id.action_forgetPasswordFragment_to_emailCheckFragment)
                                } else {
                                    Toast.makeText(requireActivity(), "账号不存在", Toast.LENGTH_SHORT).show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(binding: FragmentForgetPasswordBinding) {
        val topPanel = binding.topPanel
        topPanel.leftImage.setOnClickListener {
            publicViewModel!!.resetResetPasswordInfo()
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        val rightLayout = topPanel.rightLayout
        rightLayout.visibility = View.GONE
        topPanel.titleText.visibility = View.GONE
    }

}